<template>
  <var-popup v-model:show="dialog" position="bottom" :close-on-click-overlay="false"
    :style="{ backgroundColor: 'transparent' }">
    <div class="full-width relative-position" style="height: 300px;">
      <div class="union-box">
        <div class="union-box-text">
          <img :src="prodItem?.productLogo" alt="logo" style="width: 24px; height: 24px" />
          <span style="margin-left: 7px;font-size: 17px; font-weight: 600;">{{ prodItem?.productName }}</span>
        </div>
        <div class="style-bg"></div>
        <div class="main-box">
          <div class="main-box-body">
            <div class="body-money">
              {{ prodItem?.maxAmount || 200000 }}
            </div>
            <div class="body-tips">
              最高可借额度（元）
            </div>
          </div>
          <div style="width: 100%; margin-bottom: 24px;">
            <q-btn padding="none none" unelevated rounded @click="submit" :loading="loading" class="full-width" style="
                    background-color: #1277FF;
                    color: #fff;
                    font-size: 15px;
                    font-weight: 600;
                  ">
              <div>
                <div style="margin-top: 12px; font-size: 18px;color: #FFFFFF;line-height: 16px;">
                  确认授权并申请
                </div>
                <div class="phone">
                  系统将使用手机号{{ phoneDesensitize }}自动申请
                </div>
              </div>
            </q-btn>
          </div>
          <div>
            <div class="flex justify-center" style="flex-wrap: nowrap;">
              <div class="center q-mr-xs" @click="isApprove = !isApprove">
                <img v-if="!isApprove" src="~/assets/images/public/nu_select.png" style="width: 15px" />
                <img v-else src="~/assets/images/public/ok_select.png" style="width: 15px" />
              </div>
              <div style="font-size: 12px; opacity: 0.7; color: #000;margin-top: 1px;white-space: wrap;">
                <span @click="isApprove = true">我已阅读并同意</span>
                <template v-if="privacy.privacy && privacy.privacyLevel">
                  <a @click="openPopup(0)" href="javascript:0;"
                    style="color: #1277ff">《{{ privacy.privacy.name }}》</a><a @click="openPopup(1)" href="javascript:0;"
                    style="color: #1277ff">《{{ privacy.privacyLevel.name }}》</a>
                </template>
                <template v-for="(item, index) in prodItem?.privacy">
                  <a v-if="!(item.key == 'privacy' || item.key == 'privacyLevel')" @click="customProtocolShow(item.url)" href="javascript:0;"
                    style="color: #1277ff">《{{ item.name }}》</a>
                </template>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="close-btn">
      <img @click="close" style="width: 16px;height: 16px;" src="~/assets/images/public/close.png" alt="">
    </div>
    <AgreementDialog 
      ref="AgreementDialogRef" 
      @confirm="confirm" 
      :privacy="privacy.privacy.url" 
      :privacy-level="privacy.privacyLevel.url"
      :left-name="privacy.privacy.name || '用户服务协议'"
      :right-name="privacy.privacyLevel.name || '隐私政策'"
    />
    <CustomAgreementDialog ref="CustomAgreementDialogRef" @confirm="confirm">
    </CustomAgreementDialog>
  </var-popup>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue';
import { Show } from './UnionDialog';
import CustomAgreementDialog from 'src/components/customAgreementDialog/CustomAgreementDialog.vue'
import { CustomAgreementDialogInstance } from 'src/components/customAgreementDialog/CustomAgreementDialog'
import AgreementDialog from 'src/components/AgreementDialog.vue';
import { AgreementDialogInstance } from 'src/components/AgreementDialog';
import { ProdItem } from 'src/pages/prodList/prodList';
const dialog = ref(false);
const loading = ref(false);
const isApprove = ref(false);
const prodItem = ref<ProdItem>();
const AgreementDialogRef = ref<AgreementDialogInstance>();
const CustomAgreementDialogRef = ref<CustomAgreementDialogInstance>();
const emit = defineEmits(['confirm', 'close'])
const phone = ref(localStorage.getItem('mobile'))
const phoneDesensitize = computed(() => {
  if (!phone.value) return ''
  return `${phone.value.slice(0, 3)}****${phone.value.slice(phone.value.length - 3, phone.value.length)}`
})
const show: Show = async (item) => {
  prodItem.value = item;
  dialog.value = true;
};
const privacy = computed(() => {
  if(prodItem.value?.privacy){
    return {
      privacy: {
        url: prodItem.value.privacy.find((item) => item.key == 'privacy')?.url,
        name: prodItem.value.privacy.find((item) => item.key == 'privacy')?.name
      },
      privacyLevel: {
        url: prodItem.value.privacy.find((item) => item.key == 'privacyLevel')?.url,
        name: prodItem.value.privacy.find((item) => item.key == 'privacyLevel')?.name
      }
    }
  }
  return {
    privacy: {
      url: '',
      name: null
    },
    privacyLevel: {
      url: '',
      name: null
    }
  }
})
const submit = () => {
  if(isApprove.value){
    confirm()
  }else{
    openPopup(0)
  }
};

const confirm = () => {
  dialog.value = false;
  emit('confirm', prodItem.value)
}
const close = () => {
  dialog.value = false;
  emit('close')
};
const customProtocolShow = (url: string) => {
  CustomAgreementDialogRef.value?.show(url)
};
const openPopup = (index: number = 0) => {
  AgreementDialogRef.value?.show(index)
}
defineExpose({ show });
</script>

<style lang="scss" scoped>
.close-btn {
  position: absolute;
  right: 0;
  top: 0;
  margin: 16px;
}

.union-box {
  background-image: url(src/assets/images/public/popup-top-2.png);
  background-color: #fff;
  background-repeat: no-repeat;
  background-size: 100% auto;
  border-radius: 20px 20px 0 0;
  overflow: hidden;
  height: 100%;
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .style-bg {
    position: absolute;
    width: 100%;
    height: 182px;
    top: 60px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.95) 10%, #fff 100%);
  }

  .main-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    z-index: 11;
    padding: 6px 13px;
    flex: 1 0 auto;
    width: 100%;

    .phone {
      margin: 6px 0 12px 0;
      font-weight: 400;
      font-size: 12px;
      color: #FFFFFF;
      line-height: 16px;
      opacity: 0.5;
    }

    .body-tips {
      text-align: center;
      margin-bottom: 20px;
      font-weight: 400;
      font-size: 14px;
      color: #999999;
      line-height: 20px;
    }

    .body-money {
      background-image: url(src/assets/images/public/money_line.png);
      background-color: #fff;
      background-repeat: no-repeat;
      background-size: 100% auto;
      padding: 0 31px;
      font-weight: bold;
      font-size: 40px;
      color: #222222;
      line-height: 47px;
    }

    &-body {
      margin-top: 26px;
      position: relative;
    }
  }

  &-text {
    display: flex;
    align-items: center;
  }
}

.submit-btn {
  background-color: #1277ff;
  color: #fff !important;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 1px;
  color: #000;

  .tips {
    position: absolute;
    $width: 120px;
    width: $width;
    top: -0.225 * $width * 0.9;
    height: 0.225 * $width;
    background: url(src/assets/images/home/btn-tips.png) no-repeat;
    background-size: 100% 100%;
  }

  .pointer {
    position: absolute;
    right: calc(10% - 0px);
    top: calc(50% - 4px);
    z-index: 1;

    img {
      position: abs;
      z-index: 1;
      animation: scale-anm 2s infinite;
    }
  }
}

@keyframes scale-anm {
  0% {
    transform: scale(1);
  }

  25% {
    transform: scale(1.2);
  }

  50% {
    transform: scale(1);
  }

  75% {
    transform: scale(1.2);
  }

  100% {
    transform: scale(1);
  }
}
</style>